package com.ruoyi.word.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.CommonRedisService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.domain.word.vo.*;
import com.ruoyi.common.domain.word.*;
import com.ruoyi.word.dao.mapper.*;
import com.ruoyi.word.service.ICommonWordSentenceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommonWordSentenceServiceImpl implements ICommonWordSentenceService {

    @Autowired
    private CommonWordSentenceMapper dao;

    @Autowired
    private CommonWordMapper wordMapper;


    @Autowired
    private CommonRedisService redisService;

    @Override
    public List<CommonWordSentence> findExSentence(Long wid) {
        return dao.findExSentence(wid);
    }

    @Override
    public int saveOrUpdate(CommonWordSentenceVO record, LoginUser user) {
        // 删除旧记录
        dao.deleteByWid(record.getId());
        // 更新word信息
        wordMapper.updateByPrimaryKeySelective(buildWordNewAttr(record, user));
//        // 插入关联数据
        int sort = 1, exSenNum = 0, exSenPhNum = 0;
//        if(record.getSentences() != null && !record.getSentences().isEmpty()) {
//            for (CommonSubjectSentence s: record.getSentences()) {
//                dao.insertSelective(buildRecord(record.getId(), s, user, sort++));
//                // 更新例句的释义
//                updateSubjectSentence(s);
//                exSenNum++;
//                if(StringUtils.isNotBlank(s.getPh())){
//                    exSenPhNum++;
//                }
//            }
//        }
        // 更新单词例句数量
        wordMapper.updateExSenNum(record.getId(), exSenNum, exSenPhNum);
        return 1;
    }

    @Override
    public int saveCommonWordSen(CommonWord word, List<CommonWordSentence> sentences) {
        // 删除旧记录
        dao.deleteByWid(word.getId());
//        dao.deleteByWidSourceIsNull(word.getId());
        // 插入关联数据
        int exSenNum = 0, exSenPhNum = 0;
        if(CollectionUtils.isEmpty(sentences)) {
            return 0;
        }
        for (int i = 1; i <= sentences.size(); i++) {
            CommonWordSentence sentence = sentences.get(i - 1);
            if(StringUtils.isEmpty(sentence.getContent())){
                continue;
            }
            dao.insertSelective(buildRecord(word, sentence, i));
            if(StringUtils.isNotEmpty(sentence.getPh())){
                exSenPhNum += 1;
            }
            exSenNum++;
        }
        // 更新单词例句数量
        word.setExSenNum(exSenNum);
        word.setExSenPhNum(exSenPhNum);
        wordMapper.updateByPrimaryKeySelective(word);
        return 1;
    }

    @Override
    public boolean existsBySubjectId(Long subId) {
        return dao.existsBySubjectId(subId);
    }

    @Override
    public List<Map> countBySid(Long subId) {
        return dao.countBySid(subId);
    }

    @Override
    public int save(CommonWordSentence sentence) {
        return dao.insertSelective(sentence);
    }

//    private int updateSubjectSentence(CommonSubjectSentence s) {
//        // 释义为空，暂不处理
//        if(StringUtils.isEmpty(s.getPhRaw())) {
//            return 0;
//        }
//        CommonSubjectSentence _r = subjectSentenceMapper.selectByPrimaryKey(s.getId());
//        if(!StringUtils.equals(s.getPhRaw(), _r.getPh())) {
//            // 更新释义
//            CommonSubjectSentence ss = new CommonSubjectSentence();
//            ss.setId(s.getId());
//            ss.setPh(s.getPhRaw());
//            subjectSentenceMapper.updateByPrimaryKeySelective(ss);
//        }
//        // 更新其他关联例句的释义
//        dao.updatePhBySid(s.getPhRaw(), s.getId());
//        // 清空关联缓存
//        clearCahce(s.getSubId());
//        return 1;
//    }

//    private CommonWordSentence buildRecord(Long wid, CommonSubjectSentence sentence, LoginUser user, Integer sort) {
//        CommonWordSentence ws = new CommonWordSentence();
//        if (user == null){
//            ws.setAddUid(-1L);
//        }else {
//            ws.setAddUid(user.getUid());
//        }
//        ws.setAddTime(DateUtils.getCurrentDate());
//        ws.setWid(wid);
//        ws.setSid(sentence.getId());
//        ws.setSps(sentence.getContent());
//        ws.setPps(sentence.getPh());
//        ws.setSort(sort);
//        return ws;
//    }

    private CommonWordSentence buildRecord(CommonWord word, CommonWordSentence ws, Integer sort) {
        ws.setAddUid(-1L);
        ws.setAddTime(new Date());
        ws.setWid(word.getId());
        ws.setSort(sort);
        return ws;
    }

    private CommonWord buildWordNewAttr(CommonWordSentenceVO word, LoginUser user){
        CommonWord w = new CommonWord();
        w.setId(word.getId());
        w.setUpdateTime(new Date());
        if (user == null){
            w.setUpdateUid(-1L);
        }else {
            w.setUpdateUid(user.getUserId());
        }
        w.setSimplePh(word.getSimplePh());
        w.setCompletePh(word.getCompletePh());
        w.setNote(word.getNote());
        w.setDisassembly(word.getDisassembly());
        return w;
    }

}
